<template>
  <div class="container">

    <div class="container_body">
      <div class="container_head">
        <h2 style="font-size: 20px">{{ courseInfo.courceName }}</h2>
        <el-rate v-model="value" :colors="colors" show-text></el-rate>
      </div>
      <el-tabs type="border-card" value="secondTab">
        <el-tab-pane label="简介" name="firstTab">
          <div class="teacherinfo">
            <h3>授课教师：{{ teacherInfo.userName }}</h3>
            <h3>教学经验：{{ teacherInfo.personDesc }}</h3>
          </div>
        </el-tab-pane>
        <el-tab-pane label="目录" name="secondTab">
          <el-tree class="myTree" :data="courseDirList" :props="defaultProps" @node-click="handleNodeClick">

          </el-tree>
        </el-tab-pane>
      </el-tabs>
    </div>


    <!-- 详情弹框 -->
    <el-dialog :title="detailDialogTitle" :visible="detailDialogVisible" @close="detailDialogVisible = false">
      <a :href="url" target="_blank">{{ resourceType == "0" ? '点击下载' : '点击跳转播放' }}</a>
    </el-dialog>

    <div class="dialog1">
      <el-dialog :visible="dialogResourceTable" @close="dialogResourceTable = false">
        <div class="dialogResource">
          <el-button slot="reference" type="primary" icon="el-icon-add" v-if="user.role == 1"
                     style="float: left;position: relative;left: 10px; width: 200px;margin-bottom: 20px"
                     @click="toSelect">添加资源
          </el-button>
          <el-table
              :data="resourceList"
              border
              style="">
            <el-table-column
                fixed
                prop="resourceName"
                label="资源名"
                height="10"
                width="200">
            </el-table-column>
            <el-table-column label="发布时间"
                             width="200">
              <template slot-scope="scope">
                {{ dateFormat(scope.row.createTime) }}
              </template>
            </el-table-column>
            <el-table-column label="更新时间"
                             width="200">
              <template slot-scope="scope">
                {{ dateFormat(scope.row.updateTime) }}
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                    size="mini"
                    type="success"
                    @click="detail(scope.row)">查看详情
                </el-button>
                <el-button
                    size="mini"
                    type="danger"
                    v-if="user.role == 1"
                    @click="remove(scope.row)">删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>

        </div>

      </el-dialog>
      <!-- 详情弹框 -->
      <el-dialog :title="detailDialogTitle" :visible="detailDialogVisible" @close="detailDialogVisible = false">
        <a :href="url" target="_blank">{{ resourceType == 0 ? '点击下载' : '点击跳转播放' }}</a>
      </el-dialog>
    </div>

  </div>
</template>

<script>
export default {
  name: "CourseDirectory",
  data() {
    return {
      classes: this.$route.query.classes,
      value: null,
      colors: ['#99A9BF', '#F7BA2A', '#FF9900'],
      //课程信息
      courseInfo: '',
      //教师信息
      teacherInfo: '',
      defaultProps: {
        children: 'children',
        label: 'dirName',
      },
      //目录数据
      courseDirList: [],
      dialogResourceTable: false,
      dirId: -1,

      //一下时资源列表弹框用到的
      resourceType: 0,
      resourceList: [],
      resourceIds: [],
      detailDialogVisible: false,
      detailDialogTitle: '',
      user: JSON.parse(localStorage.getItem("user")),
      url: '',
      addDialogVisible: false,

    }
  },
  methods: {
    handleNodeClick(node) {
      const _this = this;
      //点击树节点时,如果节点的children为null则触发
      if (node.children == null) {
        _this.dialogResourceTable = true;
      }
      _this.dirId = node.dirId;
      console.log(node)
      _this.initResourceDialog()
    },

    //一下时资源列表弹框用到的
    //跳转到选择页面
    toSelect() {
      const _this = this;
      this.$router.push({
        path: '/resourceSelect',
        query: {
          dirId: _this.dirId,
          classIdid: _this.classes.classIdid,
          rePath: "/courseDirectory"
        }
      });
    },
    //显示详情
    detail(row) {
      this.detailDialogVisible = true;
      this.resourceType = row.resourceType;
      this.detailDialogTitle = row.resourceName;
      this.url = row.resourceUrl;
    },
    dateFormat(time) {
      var date = new Date(time);
      var year = date.getFullYear();
      var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
      var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
      var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
      var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
    },
    initResourceDialog() {
      const _this = this;
      axios.get("http://localhost:8080/classDirResource/findStuClassDirResource/" + _this.dirId + "/" + _this.classes.classIdid).then(function (resp) {
        _this.resourceList = resp.data.data;
        console.log("_this.resourceList")
        console.log(_this.resourceList)
      })
    },

  },
  created() {
    const _this = this;
    //加载课程信息
    axios.get("http://localhost:8080/courseMaster/findcourseByCourceId?courceId=" + _this.classes.courceId).then(function (res) {
      _this.courseInfo = res.data.data;
      console.log("课程信息");
      console.log(_this.courseInfo);
    });

    //加载课程目录
    console.log(_this.classes);
    axios.get("http://localhost:8080/courseDirectory/findCourseTreeByCourceId?courceId=" + _this.classes.courceId).then(function (res) {
      _this.courseDirList = res.data.data;
      console.log("课程目录");
      console.log(_this.courseDirList);
    });

    //加载课程详情
    axios.get("http://localhost:8080/courseDirectory/findTeacherInfoByUserId?userId=" + _this.classes.teacherId).then(function (res) {
      _this.teacherInfo = res.data.data;
      console.log("教师信息");
      console.log(_this.teacherInfo);
    });
  }
}
</script>

<style scoped>
.container {

}

.container_head {
  /*border: 1px solid red;*/
  margin: auto;
  width: 30%;
  height: 80px;
  padding: 20px;
  text-align: center;
  margin-bottom: 10px;
}

.container_head:hover {
  box-shadow: 0 20px 120px 0 rgb(69, 176, 255);
}

.container_body {
  /*border: 10px solid red;*/
  margin-top: 20px;
  height: 500px;
  width: 80%;
  margin: auto;
  padding: 20px;
}

.teacherinfo {
  color: black;
  text-align: left;
  margin-left: 30px;
}

.myTree {
  font-size: 20px;
  padding: 10px;
}

.dialog1 {
  height: 1000px;
}
</style>
